// pages/finance_account/draw_out_cash/draw_out_cash.js
import {
    wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
import util from '../../../utils/util';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        invoiceImg: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.uploadInvoiceImgImgs = this.selectComponent("#uploadDownImgs");
    },

    //选择身份证反面图片
    chooseInvoiceImg: function (e) {
        this.setData({
            invoiceImg: this.uploadInvoiceImgImgs.data.imgs
        })
    },

    deleteInvoiceImg: function (e) {
        this.setData({
            invoiceImg: this.uploadInvoiceImgImgs.data.imgs
        })
    },

    deloriginInvoiceImg: function () {
        this.setData({
            originInvoiceImg: this.uploadInvoiceImgImgs.data.originImgs
        })
    },

    // 预览图片
    // 预览图片
    previewImg: function (e) {
        console.log('预览')
        //获取当前图片的下标
        var index = e.currentTarget.dataset.index;

        //所有图片
        var list = this.data.listData.map(item => {
            return this.data.uploadsUrl + item.imgPath
        });
        console.log(this.data.list, 'list')
        wx.previewImage({
            //当前显示图片
            current: list[index],
            //所有图片
            urls: list
        })
    },

    // 提交核销优惠券
    formSubmit: function (e) {
        var formdata = e.detail.value;
        let url = '/odr/protected/account/requestWithdraw'
        let params = {}

        if (formdata.amount === '') {
            wx.showToast({
                title: '请输入提现金额!',
                icon: 'error'
            })
            return;
        }
        if (this.data.invoiceImg === '') {
            wx.showToast({
                title: '请提交发票图!',
                icon: 'error'
            })
            return;
        }
        params = {
            amount: formdata.amount,
            accId: wx.getStorageSync('accId'),
            invoicePath: this.uploadInvoiceImgImgs.data.datalist[0].imgPath,
        }

        // 请求
        _wxRequest.wxTonkenAjax({
            url: app.globalData.apiUrl + url,
            method: 'POST',
            data: params,
        }, (res) => {
            wx.showToast({
                title: '已提交提现信息！审核中~',
                icon: 'success'
            })
            setTimeout(() => {
                wx.redirectTo({
                    url: '/pages/finance_account/finance_index',
                })
            }, 1500)
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        wx.removeStorageSync('accId')
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})